package com.masterrabbit.mcslf.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.masterrabbit.common.core.utils.uuid.UUID;
import com.masterrabbit.common.core.web.domain.AjaxResult;
import com.masterrabbit.common.security.utils.SecurityUtils;
import com.masterrabbit.mcslf.contans.Constants;
import com.masterrabbit.mcslf.mapper.MerchantsMapper;
import com.masterrabbit.mcslf.mapper.ServicesMapper;
import com.masterrabbit.mcslf.pojo.Merchants;
import com.masterrabbit.mcslf.pojo.Services;
import com.masterrabbit.mcslf.service.ServicesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/service")
public class ServicesController {
    @Autowired
    ServicesService servicesService;
    @Autowired
    ServicesMapper servicesMapper;

    @Autowired
    MerchantsMapper merchantsMapper;
    @RequestMapping("/add")
    public AjaxResult addService(@RequestBody Services services){
        System.out.println(services.toString());
        services.setServiceCode(UUID.fastUUID().toString());
        services.setMerchantId(SecurityUtils.getUserId().intValue());
        Merchants merchants = merchantsMapper.selectById(SecurityUtils.getUserId().intValue());
        services.setMerchantName(merchants.getShopName());
        services.setCreatedAt(new Date());
        servicesService.save(services);
        return AjaxResult.success();
    }
//    删除单个
    @RequestMapping("/delete")
    public AjaxResult deleteService(Services services){
        servicesService.removeById(services.getServiceId());
        return AjaxResult.success();
    }
//    修改
    @RequestMapping("/update")
    public AjaxResult updateService(Services services){
        servicesService.updateById(services);
        return AjaxResult.success();
    }
//    列表查询
    @RequestMapping("/list")
    public AjaxResult getServiceList(@RequestBody HashMap<String, Object> map, HttpServletRequest request){
        IPage page = new Page<Services>((Integer)map.get("current"),(Integer)map.get("size"));
        QueryWrapper<Services> servicesQueryWrapper = new QueryWrapper<>();
        servicesQueryWrapper.eq("merchant_id",request.getHeader(Constants.CUURRENT_STORE));
        IPage page1 = servicesMapper.selectPage(page, servicesQueryWrapper);
        return AjaxResult.success(page1);
    }
    @RequestMapping("/deleteBatch")
    public AjaxResult deleteBatchService(@RequestBody List<Integer> idList){
        servicesService.removeByIds(idList);
        return AjaxResult.success();
    }//    批量删除


}
